% function input :
% gm: 1*p vector of parameters
% u : n*1 vector of variables 
% w : n*p matrix of instruments  
% lambda : scalar penalization parameter
% noncentral: n*1 vector of noncentral components

% function output :
% ftn  : the value of the test statistic

function ftn = hdm_noncentral(gm,u,w,lambda,noncentral,varargin)
ip = inputParser;
addParameter(ip, 'dm', 0);
parse(ip, varargin{:});
dm = ip.Results.dm;

% Compute the objective function
     n = size(u,1); 
if isequal(dm,1)
     mw = mean(exp(w*gm.'));
     residual = (u+noncentral/sqrt(n)).*(exp(w*gm.')-mw);
else
     residual = (u+noncentral/sqrt(n)).*(exp(w*gm.'));
end
     num = mean(residual)^2;
     den = mean((residual).^2);

     ftn = -sqrt(n*num./den) + lambda*sum(abs(gm.'));

end